import { createStore } from 'vuex'

export default createStore({
    state: {
        car_info: []
    },
    mutations: {


    },
    actions: {
        addToCar(context, payload) {
            return new Promise((resolve, reject) => {
                let oldItem = null;
                for (let item of context.state.car_info) {
                    if (item.iid === payload.iid) {
                        oldItem = item;
                    }
                }

                if (oldItem) {
                    oldItem.count += 1;
                    resolve('商品数量加1');
                } else {
                    payload.count = 1;
                    payload.checked = false;
                    context.state.car_info.push(payload);
                    resolve('加入购物车成功');
                }
            })
        }
    },
    modules: {}
})